//
//  OverlapCollectionViewCell.m
//  TT
//
//  Created by lanou3g on 16/3/14.
//  Copyright © 2016年 Super磊. All rights reserved.
//

#import "OverlapCollectionViewCell.h"


@interface OverlapCollectionViewCell ()
@property(nonatomic, assign)CGPoint beginPoint;
@end

@implementation OverlapCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addAllViews];
    }
    return self;
}

- (void)addAllViews {
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 355, 355)];
    self.imageV.userInteractionEnabled = NO;
    [self addSubview:self.imageV];
    self.picNum = [[Pic_Num alloc] initWithFrame:CGRectMake(10, 10, 80, 40)];
    [self.imageV addSubview:self.picNum];
    self.name_age = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imageV.frame), 160, 30)];
    [self addSubview:self.name_age];
    self.distance = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.name_age.frame), 160, 30)];
    self.distance.font = [UIFont systemFontOfSize:12];
    self.distance.userInteractionEnabled = NO;
    [self addSubview:self.distance];
    self.imageV.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0
                                                  green:arc4random() % 256 / 255.0
                                                   blue:arc4random() % 256 / 255.0
                                                  alpha:1];
    self.picNum.backgroundColor = [UIColor redColor];
    self.name_age.backgroundColor = [UIColor brownColor];
//    self.distance.backgroundColor = [UIColor yellowColor];
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIView class]]) {
            view.userInteractionEnabled = NO;
        }
        
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.beginPoint = [[touches anyObject] locationInView:self.superview];
//    CGPoint point = self.beginPoint;
//    point.y -= 20;
//    self.beginPoint = point;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //移动的总偏移
    CGPoint point1 = [[touches anyObject] locationInView:self.superview];
    CGFloat x = point1.x - self.beginPoint.x;
    if (x > 200) {
        [self.delegate changePicWithDirection:Right];
    } else if (x < -200) {
        [self.delegate changePicWithDirection:Left];
    } else {
        [UIView animateWithDuration:.5 animations:^{
            self.center = CGPointMake(self.superview.center.x, self.superview.center.y - 20);
        }];
    }
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point1 = [touch locationInView:self];
    CGPoint point2 = [touch previousLocationInView:self];
    
    //求移动前后 x y 的差值
    CGFloat x1 = point1.x - point2.x;
    CGFloat y1 = point1.y - point2.y;
    CGPoint center = self.center;
    center.x += x1;
    center.y += y1;
    self.center = center;
}

@end
